package com.dz.controller.visitor;

import com.dz.annotation.OperationLog;
import com.dz.bean.Manager;
import com.dz.bean.Visitor;
import com.dz.service.VisitorService;
import com.dz.util.Page;
import com.dz.util.PasswordUtil;
import com.dz.util.Result;
import com.dz.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/v/visitor")
public class VisitorController {
    @Autowired
    VisitorService visitorService;

    @GetMapping("page")
    public Result page(Page page, Visitor visitor){
        visitorService.page(page,visitor);
        return Result.success(page);
    }

    @PostMapping
    @OperationLog("添加游客")
    public Result add(@RequestBody Visitor visitor){
        // 设置默认密码并加密
        String defaultPassword = "123456"; // 默认密码
        visitor.setPassword(PasswordUtil.getEncryption(defaultPassword));

        Object obj = visitorService.add(visitor);
        return Result.success(obj);
    }

    @DeleteMapping("/{id}")
    @OperationLog("删除游客")
    public Result delete(@PathVariable("id") Integer id){
        Object obj = visitorService.delete(id);
        return Result.success(obj);
    }

    @PutMapping
    @OperationLog("修改游客信息")
    public Result edit(@RequestBody Visitor visitor){
        Object obj = visitorService.edit(visitor);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Visitor visitor){
        Object obj = visitorService.all(visitor);
        return Result.success(obj);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Object obj = visitorService.findById(id);
        return Result.success(obj);
    }

    @PutMapping("/changePassword")
    @OperationLog("游客修改密码")
    public Result changePassword(@RequestBody Map<String, String> passwordData, HttpServletRequest request) {
        try {
            // 从token中获取当前用户信息
            String token = request.getHeader("token");
            Integer visitorId = TokenUtil.getId(token);

            // 获取原密码和新密码
            String oldPassword = passwordData.get("oldPassword");
            String newPassword = passwordData.get("newPassword");

            if (oldPassword == null || newPassword == null) {
                return Result.error(400,"密码不能为空");
            }

            // 获取当前游客信息
            Visitor currentVisitor = visitorService.findById(visitorId);
            if (currentVisitor == null) {
                return Result.error(400,"游客不存在");
            }

            // 验证原密码
            if (!PasswordUtil.checkPassword(oldPassword, currentVisitor.getPassword())) {
                return Result.error(400,"原密码错误");
            }

            // 更新密码
            currentVisitor.setPassword(PasswordUtil.getEncryption(newPassword));
            visitorService.edit(currentVisitor);

            return Result.success(200,"密码修改成功");
        } catch (Exception e) {
            return Result.error(400,"密码修改失败: " + e.getMessage());
        }
    }
}
